package com.baorant.springboothelloworld.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping( "/mvc" )
public class HelloController {
    Logger logger = LoggerFactory.getLogger(HelloController.class);

    /**
     * 直接返回页面
     */
    @RequestMapping("/hello")
    public String Hello(){
        return "hello";
    }

    /**
     * 向前端页面传递参数
     */
    @RequestMapping("/hello1")
    public String hello1(ModelMap map){
        map.addAttribute("name","张三");
        return "hello1";
    }

    /**
     * 获取请求路径中传参
     */
    @RequestMapping("/hello1/{name}")
    public String hello2(ModelMap map, @PathVariable("name") String name){
        map.addAttribute("name",name);
        return "hello1";
    }

    /**
     * 获取请求url中参数
     */
    @RequestMapping("/hello2")
    public String hello3(ModelMap map, @RequestParam(value = "name", defaultValue = "张三") String name) {
        logger.debug("hello3开始");

        map.addAttribute("name", name);

        logger.debug("hello3 end {}", name);  // 使用占位符方式
        return "hello1";
    }
}


